package com.life.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import com.life.constants.Constants;
import com.life.model.LifeModel;
import com.life.utils.ImageUtil;

public class LifeAppfrm extends JFrame {

	int width = 800;

	int height = 600;

	LifeModel theModel = null;

	public LifeAppfrm(int w, int h, LifeModel model) {
		width = w;
		height = h;
		theModel = model;
		init();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void init() {
		WidgetRegistry.getInstance()
				.put(Constants.LIFE_APPLICATION_FRAME, this);

		
		this.setIconImage(ImageUtil.getImageIcon(LifeAppfrm.class,"com\\life\\view\\icons\\Life.gif").getImage());
		this.setTitle(Constants.APPLICATION_TITLE);
		
		this.addWindowFocusListener(new WindowFocusListener() {

			public void windowGainedFocus(WindowEvent arg0) {
				repaint();

			}

			public void windowLostFocus(WindowEvent arg0) {
				repaint();

			}

		});

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int pixelWidthOffset = (int) ((dim.getWidth() - width) / 2);
		int pixelHeightOffset = (int) ((dim.getHeight() - height) / 2);

		this.setBounds(pixelWidthOffset, pixelHeightOffset, width, height);
		this.getContentPane().setBounds(pixelWidthOffset, pixelHeightOffset,
				width, height);

		JSplitPane mainSplitPane = new JSplitPane();
		mainSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		mainSplitPane.setDividerSize(5);
		mainSplitPane.setOneTouchExpandable(true);
		mainSplitPane.setDividerLocation((int) (width * 2 / 3));

		this.getContentPane().add(mainSplitPane);

		JSplitPane leftSplitPane = new JSplitPane();
		leftSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		leftSplitPane.setDividerSize(1);
		leftSplitPane.setDividerLocation(height - 150);

		JTabbedPane tabbedPane = new JTabbedPane();
		mainSplitPane.setLeftComponent(leftSplitPane);
		mainSplitPane.setRightComponent(tabbedPane);

		JSplitPane statusSplitPane = new JSplitPane();
		statusSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		statusSplitPane.setDividerSize(1);
		statusSplitPane.setDividerLocation(leftSplitPane.getHeight() - 15);

		LifeCanvas lifeCanvas = new LifeCanvas(theModel);
		WidgetRegistry.getInstance().put(Constants.LIFE_CANVAS,lifeCanvas);
		TitledBorder lifeBorder = new TitledBorder("Life");

		leftSplitPane.setTopComponent(lifeCanvas);
		leftSplitPane.setBottomComponent(statusSplitPane);

		JPanel simulationPanel = new SimulationPanel();

		JPanel statusPanel = new StatusPanel();
		
		statusSplitPane.setTopComponent(statusPanel);
		statusSplitPane.setBottomComponent(simulationPanel);

		tabbedPane.addTab("Options", new BoardConfigurationPanel());
		tabbedPane.addTab("Rules", new RuleConfigurationPanel());
		tabbedPane.addTab("Statistics", new ReportsPanel());
		tabbedPane.addTab("Spawn", new GenerateConfigurationPanel());
		//tabbedPane.addTab("Templates", new TemplatePanel());
		
		if(!Constants.IS_APPLET)
		{
		    this.setResizable(false);
		}
		
		this.validate();
	}

}
